/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Chrome Better History
 * @author      DragonOfMercy <dragonofmercy@gmail.com>
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 */
var layer;
var element;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    layer = layui.layer;
    element = layui.element;
})
var now = new Date(), today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0),
    is_searching = false, loading = false;
/** 获取json文件**/
var getJson = function (key) {
    //设置请求同步editNote
    //$.ajaxSettings.async = false;
    // 从json文件中取数据
    $.getJSON("/_locales/zh_CN/messages.json", function (data) {
        $.each(data, function (k, item) {
            if (key == k) {
                return item['message'];
            }
        });
    });
    //设置请求异步
    //$.ajaxSettings.async = true;
}
/** 获取日期字符串**/
var getDateStr = function (m) {
    // 年 月 日 周
    var d = new Date()
    if (m > 0) {
        d = new Date(m)
    }
    var year = d.getFullYear()
    var month = d.getMonth() + 1
    var day = d.getDate()
    var week = d.getDay()
    switch (week) {
        case 0:
            week = '周日';
            break
        case 1:
            week = '周一';
            break
        case 2:
            week = '周二';
            break
        case 3:
            week = '周三';
            break
        case 4:
            week = '周四';
            break
        case 5:
            week = '周五';
            break
        case 6:
            week = '周六';
            break
    }
    return week + '，' + month + '月' + day + '，' + year

}
/** 获取当前语言 */
var getLanguage = function () {
    // return chrome.i18n.getMessage('language').substr(0, 2);
    return 'zh';
};

var getContainerScrollHeight = function () {
    return $('#container').prop('scrollHeight') - 939;
};

var clearSearch = function () {
    $('#container').html('');
    $('#search').val('');
    $('#history-summary-label').hide();
    $('#history-summary-search').hide();
    $('#history-summary-clear').hide();
    $('#datepicker').datetimepicker({minDate: false});
    is_searching = false;
};

var getHistoryByDay = function (day, scroll, nb) {
    scroll = scroll == undefined;
    nb = nb || 5000;
    if (is_searching) {
        clearSearch();
    }
    var dateStart = new Date(day.getFullYear(), day.getMonth(), day.getDate(), 0, 0, 0, 0);
    var dateEnd = new Date(day.getFullYear(), day.getMonth(), day.getDate(), 23, 59, 59);
    var query = {
        page: 1,
        limit: nb,
        text: '',
        startTime: dateStart.getTime(),
        endTime: dateEnd.getTime()
    };
    loading = true;
    // 查询得到历史记录
    if (!query) {
        throw new Error('Error Params：' + query);
    }
    $.ajax({
        url: '/note/v1/query',
        type: "POST",
        contentType: 'application/json',//发送格式（JSON串）
        data: JSON.stringify(query),//发送参数（JSON串）
        success: function (results) {
            historyResponse(results.data.list, dateStart, dateEnd, scroll);
        }
    });
    // chrome.history.search(query, function (results) {
    //     historyResponse(results, dateStart, dateEnd, scroll);
    // });
};

var search = function () {
    var text = $('#search').val();
    if (text) {
        $('#datepicker').datetimepicker({minDate: today});
        is_searching = true;
        $('#container').html('<div class="loading"></div>');
        var dateStart = new Date(1970, 1, 1, 0, 0, 0, 0);
        var dateEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 59, 59);
        var query = {
            page: 1,
            limit: 5000,
            text: text,
            startTime: dateStart.getTime(),
            endTime: dateEnd.getTime()
        };
        $('#history-summary-label').css('display', 'inline-block');
        $('#history-summary-search').html(text).css('display', 'inline-block');
        $('#history-summary-clear').css('display', 'inline-block');
        loading = true;
        $.ajax({
            url: '/note/v1/query',
            type: "POST",
            contentType: 'application/json',//发送格式（JSON串）
            data: JSON.stringify(query),//发送参数（JSON串）
            success: function (results) {
                historyResponse(results.data.list, dateStart, dateEnd, false);
            }
        });
        // chrome.history.search(query, function (results) {
        //     historyResponse(results, dateStart, dateEnd, false);
        // });
    } else {
        if (is_searching) {
            clearSearch();
        }
        getHistoryByDay(today);
    }
};

var historyResponse = function (results, start, end, scroll) {
    var datas = {}, item_date, item_date_day;
    $.each(results, function (k, v) {
        item_date = new Date(v.createTime);
        item_date_day = new Date(item_date.getFullYear(), item_date.getMonth(), item_date.getDate(), 0, 0, 0, 0).getTime();
        if (start != undefined && end != undefined && !(item_date >= start && item_date <= end)) {
            return;
        }
        if (!datas[item_date_day]) {
            datas[item_date_day] = {};
        }
        datas[item_date_day][v.createTime] = [v.nid, v.text, v.createTime];
    });
    if ($.isEmptyObject(datas)) {
        datas[start.getTime()] = {};
        datas[start.getTime()]['empty'] = [];
    }
    $.each(datas, function (k, v) {
        var output = '';
        if (!$('#container #' + k).length) {
            output += '<div class="entry" id="' + k + '">';
        }
        output += '<h2>' + getDateStr(parseFloat(k)) + '</h2>';
        $.each(v, function (id, item) {
            if (id != 'empty') {
                var removeStr = getJson('history_remove_single')
                if (removeStr == undefined) {
                    removeStr = '删除条目'
                }
                item[1] = item[1].replaceAll('&lt;', '<').replaceAll('&gt;', '>')
                output += '<span class="row" id="' + item[0] + '">';
                output += '<a class="remove-single"  onclick="removeNote(' + item[0] + ')"  title="' + removeStr + '">тип</a>';
                output += '<span class="date">' + item[2].split(" ")[1] + '</span>';
                //output += '<a class="link" href="' + escapeHtml(item[2]) + '" target="_blank" style="' + getFavicon(item[2]) + '">' + escapeHtml(item[1] ? item[1] : item[2]) + '</a>';
                output += '<a class="link" target="_blank" onclick="editNote(' + item[0] + ')">' + item[1] + '<input id="text' + item[0] + '"  type="hidden" value="' + item[1] + '"/>'+ '<input id="createTime' + item[0] + '"  type="hidden" value="' + item[2] + '"/></a>';
                output += '</span>';
                output += '<br/>';
            } else {
                var none = getJson('history_date_empty')
                if (none == undefined) {
                    none = "这一天没有记录"
                }
                output += '<span class="row empty"><span>';
                output += none;
                output += '</span></span>';
            }
        });
        if (!$('#container #' + k).length) {
            output += '</div>';
        }
        if ($('#container #' + k).length) {
            $('#' + k).html(output);
        } else {
            if ($('#container > .entry').length) {
                $('#container > .entry').each(function () {
                    if ($(this).is(':last-child')) {
                        if (k < $(this).attr('id')) {
                            $(output).insertAfter($(this));
                        } else {
                            $(output).insertBefore($(this));
                        }
                        return false;
                    } else if (k > $(this).attr('id')) {
                        if (k < $(this).attr('id')) {
                            $(output).insertAfter($(this));
                        } else {
                            $(output).insertBefore($(this));
                        }
                        return false;
                    }
                });
            } else {
                $('#container').append(output);
            }
        }
        if (scroll && $('#container #' + k).length) {
            $('#container').scrollTo('#' + k, {offsetTop: 91, duration: 0});
        }
        $('.remove-single', '#' + k).on('click', function () {
            // 删除笔记
            //chrome.history.deleteUrl({url: $(this).parent().find('.link').attr('href')});
        })
    });
    if (is_searching && $('#container .loading').length) {
        $('#container .loading').remove();
    }
    if ($('body.popup').length) {
        $('html, body').height($('.sizable').height());
    }
    loading = false;

    $('#search').focus();
};

/** 删除笔记 **/
var removeNote = function (nId) {
    layer.confirm('确认删除吗？', function () {
        // 向服务端发送删除指令
        console.log('删除笔记>>>' + nId)
        var query = {
            nId: nId
        };
        $.ajax({
            url: '/note/v1/remove?nId=' + nId,
            type: "POST",
            contentType: 'application/json',//发送格式（JSON串）
            data: JSON.stringify(query),//发送参数（JSON串）
            success: function (results) {
                if (results.data) {
                    layer.msg("删除成功", {icon: 1, time: 2000}, function () {
                        // 关闭当前页面
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        getHistoryByDay(today);
                        //重新加载父页面
                        parent.location.reload();
                    });
                } else {
                    layer.msg("删除失败", {icon: 1, time: 2000}, function () {
                        // 关闭当前页面
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        getHistoryByDay(today);
                        //重新加载父页面
                        parent.location.reload();
                    });
                }
                $(this).parent().remove();
            }
        });
    });
}

/** 弹出编辑笔记 **/
var editNote = function (nId) {
    console.log(nId, createTime)
    var text = $('#text' + nId).val()
    var createTime = $('#createTime' + nId).val()
    if (text == undefined || text == null) {
        text = ''
    }
    var url = 'edit1?nId=' + nId + '&text=' + text + '&createTime=' + createTime
    layer.open({
        type: 2, //Layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）,
        shade: 0.1, //遮罩层透明度
        area: ['850px', '500px'], //弹出层宽高
        title: '编辑',//弹出层标题
        content: url //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    });
}

/** 弹出导出 **/
var exportNote = function () {
    var url = 'export'
    layer.open({
        type: 2, //Layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）,
        shade: 0.1, //遮罩层透明度
        area: ['850px', '500px'], //弹出层宽高
        title: '导出',//弹出层标题
        content: url //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    });
}


$(document).ready(function () {

    // Load and replace language
    $('[i18n]').each(function () {
        var i18n = $(this).attr('i18n');
        if (i18n.indexOf(':') >= 0) {
            var tmp = i18n.split(':');
            // $(this).attr(tmp[0], chrome.i18n.getMessage(tmp[1]));
            $(this).attr(tmp[0], getJson(tmp[1]));
        } else {
            // $(this).html(chrome.i18n.getMessage(i18n));
            $(this).html(getJson(i18n));

        }
    });

    if ($('body.popup').length) {
        getHistoryByDay(today, false, 10);
        $('#view-full-history').on('click', function () {
            console.log('chrome://history')
            // chrome.tabs.create({url: 'chrome://history'});
        });
    } else {
        $('#datepicker').datetimepicker({
            timepicker: false,
            dayOfWeekStart: parseInt(getJson('date_week_start')),
            value: new Date(),
            maxDate: today,
            inline: true,
            todayButton: false,
            lang: getLanguage(),
            onSelectDate: function (date) {
                getHistoryByDay(date);
            }
        });

        getHistoryByDay(today);

        $('#search').on('keyup', function (e) {
            if (e.keyCode == 13) {
                search();
            }
        }).on('search', function () {
            search();
        });

        $('#search-btn').on('click', function () {
            search();
        });

        $('#container').on('scroll', function () {
            if (!is_searching && !loading) {
                if (($(this)[0].scrollTop) >= getContainerScrollHeight() * 0.9) {
                    var last = new Date(parseFloat($('#container .entry:last-child').attr('id')));
                    last.setDate(last.getDate() - 1);
                    if ($('#' + last.getTime()).length == 0) {
                        getHistoryByDay(last, false);
                    }
                }
            }
        });

        $('#datepicker-today').on('click', function () {
            $('#datepicker').datetimepicker({value: new Date()});
            getHistoryByDay(today);
        });

        $('#search').focus();
    }

    $('#history-summary-clear').on('click', function (e) {
        e.preventDefault();
        if (confirm(getJson('search_clear_confirm'))) {
            $('.remove-single').each(function () {
                $(this).click();
            });
        }
    })

});

$.fn.scrollTo = function (target, options, callback) {
    if (typeof options == 'function' && arguments.length == 2) {
        callback = options;
        options = target;
    }
    var settings = $.extend({
        scrollTarget: target,
        offsetTop: 50,
        duration: 100,
        easing: 'swing'
    }, options);
    return this.each(function () {
        var scrollPane = $(this);
        var scrollTarget = (typeof settings.scrollTarget == "number") ? settings.scrollTarget : $(settings.scrollTarget);
        var scrollY = (typeof scrollTarget == "number") ? scrollTarget : scrollTarget.offset().top + scrollPane.scrollTop() - parseInt(settings.offsetTop);
        scrollPane.animate({scrollTop: scrollY}, parseInt(settings.duration), settings.easing, function () {
            if (typeof callback == 'function') {
                callback.call(this);
            }
        });
    });
};

var dateFormat = function () {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
        timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
        timezoneClip = /[^-+\dA-Z]/g,
        pad = function (val, len) {
            val = String(val);
            len = len || 2;
            while (val.length < len) val = "0" + val;
            return val;
        };

    // Regexes and supporting functions are cached through closure
    return function (date, mask, utc) {
        var dF = dateFormat;

        // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
        if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
            mask = date;
            date = undefined;
        }

        // Passing date through Date applies Date.parse, if necessary
        date = date ? new Date(date) : new Date;
        if (isNaN(date)) throw SyntaxError("invalid date");

        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        // Allow setting the utc argument via the mask
        if (mask.slice(0, 4) == "UTC:") {
            mask = mask.slice(4);
            utc = true;
        }

        var _ = utc ? "getUTC" : "get",
            d = date[_ + "Date"](),
            D = date[_ + "Day"](),
            m = date[_ + "Month"](),
            y = date[_ + "FullYear"](),
            H = date[_ + "Hours"](),
            M = date[_ + "Minutes"](),
            s = date[_ + "Seconds"](),
            L = date[_ + "Milliseconds"](),
            o = utc ? 0 : date.getTimezoneOffset(),
            flags = {
                d: d,
                dd: pad(d),
                ddd: dF.i18n.dayNames[D],
                dddd: dF.i18n.dayNames[D + 7],
                m: m + 1,
                mm: pad(m + 1),
                mmm: dF.i18n.monthNames[m],
                mmmm: dF.i18n.monthNames[m + 12],
                yy: String(y).slice(2),
                yyyy: y,
                h: H % 12 || 12,
                hh: pad(H % 12 || 12),
                H: H,
                HH: pad(H),
                M: M,
                MM: pad(M),
                s: s,
                ss: pad(s),
                l: pad(L, 3),
                L: pad(L > 99 ? Math.round(L / 10) : L),
                t: H < 12 ? "a" : "p",
                tt: H < 12 ? "am" : "pm",
                T: H < 12 ? "A" : "P",
                TT: H < 12 ? "AM" : "PM",
                Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
                o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
                S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
            };

        return mask.replace(token, function ($0) {
            return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
}();

// Some common format strings
dateFormat.masks = {
    "default": "ddd mmm dd yyyy HH:MM:ss",
    shortDate: "m/d/yy",
    mediumDate: "mmm d, yyyy",
    longDate: "mmmm d, yyyy",
    fullDate: "dddd, mmmm d, yyyy",
    shortTime: "h:MM TT",
    mediumTime: "h:MM:ss TT",
    longTime: "h:MM:ss TT Z",
    isoDate: "yyyy-mm-dd",
    isoTime: "HH:MM:ss",
    isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
    // dayNames: chrome.i18n.getMessage('date_day_names').split('|'),
    // monthNames: chrome.i18n.getMessage('date_month_names').split('|')
    // dayNames: getJson('date_day_names').split('|'),
    // monthNames: getJson('date_month_names').split('|')
    dayNames: function () {
        var key = 'date_day_names';
        var value = '';
        $.getJSON("/_locales/zh_CN/messages.json", function (data) {
            for (var k in data) {
                if (key == k) {
                    value = data[k]['message'];
                }
            }
            return value.split('|')
        });
    },
    monthNames: function () {
        var key = 'date_month_names';
        var value = '';
        $.getJSON("/_locales/zh_CN/messages.json", function (data) {
            for (var k in data) {
                if (key == k) {
                    value = data[k]['message'];
                }
            }
            return value.split('|')
        });
    }
};

// For convenience...
Date.prototype.format = function (mask, utc) {
    return dateFormat(this, mask, utc);
};
